﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using MSAcademic.v2.Infrastructure.AccessRights;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.ContentSection;
using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.Tag;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Article
{
    public partial class ArticleUpdateModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        public string ShortText { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        public string LongText { get; set; }

        public DateTimeOffset? PublishedOn { get; set; }
        public bool IsHidden { get; set; }
        public bool PublishOnUpdate { get; set; }
        public string TagsToBindCsv { get; set; }
        public string SubSectionsToBindCsv { get; set; }
    }
    public partial class ArticleUpdateModel
    {
        public IArticle Article
        {
            get
            {
                if (_article == null)
                {
                    _article = new Lazy<IArticle>(() => _articleQueries.GetArticle(Id));
                }
                return _article.Value;
            }
        }
        private Lazy<IArticle> _article;

        public List<Tuple<IContentSection,List<ISubSection>>> ContentSectionsWithSubSections
        {
            get
            {
                if (_contentSectionsWithSubSections == null)
                {
                    _contentSectionsWithSubSections = new Lazy<List<Tuple<IContentSection, List<ISubSection>>>>(() => _contentSectionQueries.GetAllContentSectionsWithSubSections(false));
                }
                return _contentSectionsWithSubSections.Value;
            }
        }
        private Lazy<List<Tuple<IContentSection,List<ISubSection>>>> _contentSectionsWithSubSections;

        public List<ISubSection> BoundSubSections
        {
            get
            {
                if (_boundSubSections == null)
                {
                    _boundSubSections = new Lazy<List<ISubSection>>(() => _articleQueries.GetSubSectionsForArticle(Id,false).ToList());
                }
                return _boundSubSections.Value;
            }
        }
        private Lazy<List<ISubSection>> _boundSubSections;

        public List<ITag> BoundTags
        {
            get
            {
                if (_boundTags == null)
                {
                    _boundTags = new Lazy<List<ITag>>(() => _articleQueries.GetTagsForArticle(Article.Id).ToList());
                }
                return _boundTags.Value;
            }
        }
        private Lazy<List<ITag>> _boundTags;

        public List<ITag> AllTags
        {
            get
            {
                if (_allTags == null)
                {
                    _allTags = new Lazy<List<ITag>>(() => _tagQueries.GetAll().ToList());
                }
                return _allTags.Value;
            }
        }
        private Lazy<List<ITag>> _allTags;

    }

    public partial class ArticleUpdateModel
    {
        public IUser SignedInUser
        {
            get
            {
                if (_signedInUser == null)
                {
                    _signedInUser = new Lazy<IUser>(() => _loginService.TryGetSignedInUser());
                }
                return _signedInUser.Value;
            }
        }
        private Lazy<IUser> _signedInUser;

       public bool CanUpdate
        {
            get
            {
                if (_canUpdate == null)
                {
                    _canUpdate = new Lazy<bool>(() => this.SignedInUser != null && _accessRightsService.CanUserAccess(this.SignedInUser, this.Article.UserId, AccessRight.CanUpdate));
                }
                return _canUpdate.Value;
            }
        }
        private Lazy<bool> _canUpdate;

        public bool CanPublishAndHide
        {
            get
            {
                if (_canPublishAndHide == null)
                {
                    _canPublishAndHide = new Lazy<bool>(() => this.SignedInUser != null && _accessRightsService.CanUserAccess(this.SignedInUser, AccessRight.CanPublishAndHide));
                }
                return _canPublishAndHide.Value;
            }
        }
        private Lazy<bool> _canPublishAndHide;
    }

    public partial class ArticleUpdateModel
    {
        private readonly IArticleQueries _articleQueries;
        private readonly ITagQueries _tagQueries;
        private readonly IContenSectionQueries _contentSectionQueries;
        private readonly ILoginService _loginService;
        private readonly IAccessRightsService _accessRightsService;


        public ArticleUpdateModel(
            IArticleQueries articleQueries,
            ITagQueries tagQueries,
            IContenSectionQueries contenSectionQueries,
            ILoginService loginService,
            IAccessRightsService accessRightsService)
        {
            _articleQueries = articleQueries;
            _tagQueries = tagQueries;
            _contentSectionQueries = contenSectionQueries;
            _loginService = loginService;
            _accessRightsService = accessRightsService;
        }

        public ArticleUpdateModel()
        {

        }
    }
}